package com.kyzh.core.pager.weal.recover

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.util.setMargins
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.R
import com.kyzh.core.adapters.decoration.SpacesItemDecoration
import com.kyzh.core.current.RecoverBean
import com.kyzh.core.current.WealImpl1.WealImpl1
import com.kyzh.core.databinding.ItemRecoverBinding
import com.kyzh.core.utils.setVisibility
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.activity_recover.*

class RecoverActivity : BaseActivity() {
    private  var recoverBean :MutableList<RecoverBean.Data>?  = mutableListOf()
    private  var adapter : RecoverAdapter = RecoverAdapter(recoverBean)
    var small_uid : String = ""
    var mPersion : Int  = -1
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, RecoverActivity::class.java)
            context.startActivity(starter)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recover)
        initView()
        getData()

    }

    fun getData(){
        WealImpl1.getRecover{
            tvContent.text = this?.config?.content
            tvContentTip.text = this?.config?.content2
            recoverBean  = this?.data
            if(recoverBean.isNullOrEmpty()){
                tvSubmit1.setVisibility(false)
            }else{
                tvSubmit1.setVisibility(true)
            }
            adapter.setNewInstance(recoverBean)
            adapter.notifyDataSetChanged()

            val inflate = LayoutInflater.from(this@RecoverActivity).inflate(R.layout.empty, null)
            val findViewById = inflate.findViewById<ImageView>(R.id.image)
            val tv1 = inflate.findViewById<TextView>(R.id.tv1)
            tv1.text = "暂无小号记录"
            findViewById.setImageResource(R.drawable.icon_empty_recover)
            adapter.setEmptyView(inflate)
            adapter.setOnItemClickListener { adapter, view, position ->
                recoverBean?.apply {
                    forEach {
                        it.check = false
                    }
                    if(get(position).small_uid.isNullOrEmpty()){
                        "该小号存在异常".toast()
                        setSubmitButtonState(false)
                        return@setOnItemClickListener
                    }
                    small_uid =  get(position).small_uid
                    mPersion = position
                    get(position).check = true
                }
                adapter.notifyDataSetChanged()
                setSubmitButtonState(true)

            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        getData()
    }

    fun initView(){
        close.setOnClickListener{
            finish()
        }
        titleRight.setOnClickListener {
            RecoverRecordActivity.start(this)
        }
        setSubmitButtonState(false)
        rcvAmount.addItemDecoration(SpacesItemDecoration(10f))
        rcvAmount.layoutManager =  LinearLayoutManager(this, RecyclerView.VERTICAL,false)
        rcvAmount.adapter = adapter
        tvSubmit1.setOnClickListener {
            WealImpl1.recoverSub(small_uid){
                if(this){
                    adapter.removeAt(mPersion)
                    adapter.notifyDataSetChanged()
                }
            }
        }
    }

    /**
     * 设置提交按钮状态
     */
    fun setSubmitButtonState(boolean: Boolean){
        tvSubmit1.isEnabled = boolean
        if(boolean){
            tvSubmit1.setBackgroundResource(R.drawable.background_blue_recover_check)
        }else{
            tvSubmit1.setBackgroundResource(R.drawable.background_blue_recover_nochack)
        }
    }


    inner class RecoverAdapter(recoverBean :MutableList<RecoverBean.Data>?):BaseQuickAdapter<RecoverBean.Data,  BaseDataBindingHolder<ItemRecoverBinding>>(R.layout
        .item_recover,recoverBean){
        override fun convert(holder:  BaseDataBindingHolder<ItemRecoverBinding>, item: RecoverBean.Data) {
            item.huishoujia = "回收价："
            holder.dataBinding?.apply {
                imgAccount.setMargins(0,0,0,0,)
                tvTimeTip.setVisibility(false)
                tvTime.setVisibility(false)
                tvState.setVisibility(false)
                data = item
            }
        }
    }

}